package com.knowledgegraph.util;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 密码工具类
 */
public class PasswordUtil {
    
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("请提供要加密的密码");
            return;
        }
        
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String rawPassword = args[0];
        String encodedPassword = encoder.encode(rawPassword);
        
        System.out.println("原始密码: " + rawPassword);
        System.out.println("加密后密码: " + encodedPassword);
        
        // 验证加密结果
        boolean matches = encoder.matches(rawPassword, encodedPassword);
        System.out.println("验证结果: " + matches);
        
        // 测试数据库中现有的哈希
        String existingHash = "$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lbdxIo0jAdOr6Wq2.";
        boolean existingMatches = encoder.matches(rawPassword, existingHash);
        System.out.println("现有哈希验证: " + existingMatches);
        
        // 生成一个新的标准哈希用于更新数据库
        System.out.println("\n建议使用的SQL更新语句:");
        System.out.println("UPDATE users SET password = '" + encodedPassword + "' WHERE username = 'admin';");
    }
}